package cn.slipi.admin.controller;

import cn.slipi.admin.biz.MenuBiz;
import cn.slipi.admin.biz.vo.menu.AuthMenuVo;
import cn.slipi.admin.biz.vo.menu.MenuVo;
import cn.slipi.admin.common.annotation.Permission;
import cn.slipi.admin.common.response.JsonResponse;
import cn.slipi.admin.constants.AdminConstants;
import cn.slipi.admin.constants.permission.PermissionConstants;
import cn.slipi.admin.controller.form.menu.MenuSaveForm;
import cn.slipi.admin.entity.TSysMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 菜单接口
 *
 * @author lee
 * @date 2019-03-17
 * @since 1.0.0
 */


@Validated
@Controller
@ResponseBody
@RequestMapping(AdminConstants.CAR_API_PREFIX  + "/menu")
public class MenuController {

    @Autowired
    private MenuBiz menuBiz;

    /**
     * 获取菜单列表（侧边栏）
     *
     * @return
     */
    @GetMapping("/list/tree")
    @Permission(value = {PermissionConstants.MENU_GET_MENU_LIST_TREE})
    public JsonResponse<List<MenuVo>> getMenuListTree() {
        JsonResponse<List<MenuVo>> responseVo = new JsonResponse<>();
        List<MenuVo> result = this.menuBiz.getMenuListTree();
        responseVo.setData(result);
        return responseVo;
    }

    /**
     * 菜单列表（角色菜单授权）
     *
     * @param rid
     * @return
     */
    @GetMapping("/list/auth/{rid}")
    @Permission(value = {PermissionConstants.MENU_GET_MENU_FOR_AUTH})
    public JsonResponse<List<AuthMenuVo>> getMenuListForAuth(@PathVariable("rid") Integer rid) {
        JsonResponse<List<AuthMenuVo>> responseVo = new JsonResponse<>();
        List<AuthMenuVo> result = this.menuBiz.getMenuListAuth(rid);
        responseVo.setData(result);
        return responseVo;
    }

    /**
     * 获取菜单列表（菜单管理）
     *
     * @return
     */
    @GetMapping("/list")
    @Permission(value = {PermissionConstants.MENU_GET_MENU_LIST})
    public JsonResponse<List<TSysMenu>> getMenuList() {
        JsonResponse<List<TSysMenu>> responseVo = new JsonResponse<>();
        List<TSysMenu> result = this.menuBiz.getMenuList();
        responseVo.setData(result);
        return responseVo;
    }

    /**
     * 获取顶级菜单列表（下拉列表使用）
     *
     * @return
     */
    @GetMapping("/list/top")
    @Permission(value = {PermissionConstants.MENU_GET_TOP_MENU_LIST})
    public JsonResponse<List<TSysMenu>> getTopMenuList() {
        JsonResponse<List<TSysMenu>> responseVo = new JsonResponse<>();
        List<TSysMenu> result = this.menuBiz.getTopMenuList();
        responseVo.setData(result);
        return responseVo;
    }


    /**
     * 添加菜单
     *
     * @param menuSaveForm
     * @return
     */
    @PostMapping("/add")
    @Permission(value = {PermissionConstants.MENU_ADD_MENU})
    public JsonResponse<Integer> addMenu(@Valid @RequestBody MenuSaveForm menuSaveForm) {
        JsonResponse<Integer> responseVo = new JsonResponse<>();
        Integer result = this.menuBiz.addMenu(menuSaveForm);
        responseVo.setData(result);
        return responseVo;
    }


    /**
     * 更新菜单
     *
     * @param mid
     * @param menuSaveForm
     * @return
     */
    @PutMapping("/update/{mid}")
    @Permission(value = {PermissionConstants.MENU_UPDATE_MENU})
    public JsonResponse<Boolean> updateMenu(@PathVariable("mid") Integer mid, @Valid @RequestBody MenuSaveForm menuSaveForm) {
        JsonResponse<Boolean> responseVo = new JsonResponse<>();
        Boolean result = this.menuBiz.updateMenu(mid, menuSaveForm);
        responseVo.setData(result);
        return responseVo;
    }

    /**
     * 删除菜单
     *
     * @param mid
     * @return
     */
    @DeleteMapping("/delete/{mid}")
    @Permission(value = {PermissionConstants.MENU_DELETE_MENU})
    public JsonResponse<Boolean> deleteMenu(@PathVariable("mid") Integer mid) {
        JsonResponse<Boolean> responseVo = new JsonResponse<>();
        Boolean result = this.menuBiz.deleteMenu(mid);
        responseVo.setData(result);
        return responseVo;
    }

}
